<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="案件号" prop="evtCode">
        <el-input
          style="width: 219px"
          v-model="queryParams.evtCode"
          placeholder="请输入案件号"
          clearable
          size="small"
          oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="案件类型" prop="evtType">
        <el-select v-model="queryParams.evtType" placeholder="请选择案件类型" clearable size="small">
          <el-option
            v-for="dict in evtTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="案件来源" prop="evtSource" v-if="showEvtSource">
        <el-select v-model="queryParams.evtSource" placeholder="请选择案件来源" clearable size="small"
                   :disabled="evtSource!==undefined?true:false">
          <el-option
            v-for="dict in evtSourceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="案件状态" prop="evtState">
        <el-select v-model="queryParams.evtState" placeholder="请选择案件状态" clearable size="small">
          <el-option
            v-for="dict in evtStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="违法、违规信息" prop="violation" label-width="110px">
        <!--        <el-select v-model="queryParams.violation" placeholder="请选择违法、违规信息" clearable size="small">-->
        <!--          <el-option-->
        <!--            v-for="dict in violationOptions"-->
        <!--            :key="dict.dictValue"-->
        <!--            :label="dict.dictLabel"-->
        <!--            :value="dict.dictValue"-->
        <!--          />-->
        <!--        </el-select>-->
        <!--        <el-tree-->
        <!--          :data="violationOptions"-->
        <!--          :props="defaultProps"-->
        <!--          :expand-on-click-node="false"-->
        <!--          :filter-node-method="filterNode"-->
        <!--          ref="tree"-->
        <!--          default-expand-all-->
        <!--          @node-click="handleNodeClick"-->
        <!--        />-->
        <treeselect v-model="queryParams.violation" :options="violationOptions" :show-count="true"
                    placeholder="请选择违法、违规信息" style="width:300px;"/>
      </el-form-item>
      <el-form-item label="管辖区" prop="areaName">
        <el-select v-model="queryParams.areaName" placeholder="请选择管辖区" clearable size="small">
          <el-option
            v-for="dict in areaNameOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="举报时间" prop="reporterTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.reporterTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择举报时间"
                        :picker-options="pickerOptions"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['frontierEvent:event:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['frontierEvent:event:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['frontierEvent:event:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="eventList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="主建id" align="center" prop="id" v-if="false"/>
      <el-table-column label="案件号" align="center" prop="evtCode"/>
      <el-table-column label="案件等级" align="center" prop="evtLevel" :formatter="evtLevelFormat"/>
      <el-table-column label="案件类型" align="center" prop="evtType" :formatter="evtTypeFormat"/>
      <el-table-column label="案件来源" align="center" prop="evtSource" :formatter="evtSourceFormat"/>
      <el-table-column label="案件地点" align="center" prop="evtAddress"/>
      <el-table-column label="案件内容" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-html="scope.row.evtContent">{{scope.row.evtContent}}</span>
        </template>
      </el-table-column>
      <el-table-column label="事发现场图片" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="showImage(scope.row)"
            v-hasPermi="['frontierEvent:event:edit']"
          >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="案件状态" align="center" prop="evtState" :formatter="evtStateFormat"/>
      <el-table-column label="违法、违规信息" align="center" prop="violationName"/>
      <el-table-column label="管辖区" align="center" prop="areaName" :formatter="areaNameFormat"/>
      <el-table-column label="举报人" align="center" prop="reporterObjectName"/>
      <el-table-column label="举报时间" align="center" prop="reporterTime" width="180"/>
      <el-table-column label="处置结果" align="center" prop="management"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['frontierEvent:event:edit']"
          >办理
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['frontierEvent:event:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改案件信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="案件等级" prop="evtLevel">
              <el-select v-model="form.evtLevel" placeholder="请选择案等级" ref="evtLevel">
                <el-option
                  v-for="dict in evtLevelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">

            <el-form-item label="案件类型" prop="evtType">
              <el-select v-model="form.evtType" placeholder="请选择案件类型">
                <el-option
                  v-for="dict in evtTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="案件来源" prop="evtSource">
              <el-select v-model="form.evtSource" placeholder="请选择案件来源">
                <el-option
                  v-for="dict in evtSourceOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="案件时间" prop="evtDate">
              <el-date-picker clearable size="small"
                              v-model="form.evtDate"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择案件时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="案件地点" prop="evtAddress">
          <el-input v-model="form.evtAddress" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="案件内容">
          <el-input v-model="form.evtContent" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="案件状态" prop="evtState">
              <el-select v-model="form.evtState" placeholder="请选择案件状态">
                <el-option
                  v-for="dict in evtStateOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管辖区" prop="areaName">
              <el-select v-model="form.areaName" placeholder="请选择管辖区">
                <el-option
                  v-for="dict in areaNameOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="违法、违规信息" prop="violation">
          <el-row v-if="form.violation === 0">
            <treeselect v-model="form.violation" :options="violationOptions" :show-count="true"
                        placeholder="请选择违法、违规信息"/>
          </el-row>
          <el-row v-else>
            <treeselect v-model="form.violation" :options="violationOptions" :show-count="true"
                        placeholder="请选择违法、违规信息"/>
          </el-row>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="X坐标" prop="x">
              <el-input v-model="form.x" placeholder="请输入X坐标"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="Y坐标" prop="y">
              <el-input v-model="form.y" placeholder="请输入Y坐标"/>
            </el-form-item>
          </el-col>
        </el-row>


        <!--        <el-form-item label="举报人ID" prop="reporterObjectId">-->
        <!--          <el-input v-model="form.reporterObjectId" placeholder="请输入举报人ID" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="举报人" prop="reporterObjectName">-->
        <!--          <el-input v-model="form.reporterObjectName" placeholder="请输入举报人" />-->
        <!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="举报时间" prop="reporterTime">
              <el-date-picker clearable size="small"
                              v-model="form.reporterTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              :picker-options="pickerOptions"
                              placeholder="选择举报时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处置结果">
              <el-input v-model="form.management" type="textarea" placeholder="请输入处置结果"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible" title="预览" class="imgss" height="800" z-index="20">
      <viewer :images="imgs">
        <img v-for="src in imgs" :key="src.url" :src="src.url">
      </viewer>
      <!--    <img :src="value" style="display: block; max-width: 100%; margin: 0 auto;">-->
    </el-dialog>
  </div>
</template>

<script>
  import {
    listEvent,
    getEvent,
    delEvent,
    addEvent,
    updateEvent,
    exportEvent,
    exportSFEvent
  } from '@/api/frontierEvent/event'
  import Editor from '@/components/Editor'
  import {treeselect} from '@/api/basicInformation/violationType'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import Vue from 'vue'
  import Viewer from 'v-viewer'
  import 'viewerjs/dist/viewer.css'

  Vue.use(Viewer)
  Viewer.setDefaults({
    Options: {
      'inline': false,
      'button': true,
      'navbar': true,
      'title': true,
      'toolbar': true,
      'tooltip': true,
      'movable': true,
      'zoomable': true,
      'rotatable': true,
      'scalable': true,
      'transition': true,
      'fullscreen': true,
      'keyboard': true,
      'url': 'data-source'
    }
  })
  export default {
    name: 'Event',
    components: {
      Editor,
      Treeselect
    },
    props: {
      evtSource: {
        type: String,
        default: undefined
      }
    },
    data() {
      return {
        dialogVisible: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 案件信息表格数据
        eventList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 案件类型字典
        evtTypeOptions: [],
        // 案件来源字典
        evtSourceOptions: [],
        // 案件状态字典
        evtStateOptions: [],
        // 违法、违规信息字典
        violationOptions: [],
        // 管辖区字典
        areaNameOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          evtCode: undefined,
          evtType: undefined,
          evtSource: undefined,
          evtState: undefined,
          violation: undefined,
          areaName: undefined,
          reporterTime: undefined,
          isAsc: 'desc',
          orderByColumn: 'reporterTime'
        },
        showEvtSource: true,
        // 表单参数
        form: {},
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        // 表单校验
        rules: {
          evtSource: [
            {required: true, message: '案件来源不能为空', trigger: 'change'}
          ],
          evtState: [
            {required: true, message: '案件状态不能为空', trigger: 'change'}
          ]
        },
        imgs: [
          {
            url: 'http://192.168.20.113:8080/files/view?id=60b0996d78098f1deff7465c',
            title: '图片1'
          },
          {
            url: 'http://192.168.20.113:8080/files/view?id=60b0996d78098f1deff7465c',
            title: '图片2'
          }
        ],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now() - 8.64e6;
          }
        },
        fileUrl: process.env.VUE_APP_BASE_API + '/files/view?id=', // 上传的图片服务器地址,
        // 案件等级字典
        evtLevelOptions: [],
      }
    },
    created() {
      this.getList()
      this.getTreeselect()
      this.getDicts('evt_type').then(response => {
        this.evtTypeOptions = response.data
      })
      this.getDicts('evt_source').then(response => {
        this.evtSourceOptions = response.data
      })
      this.getDicts('evt_state').then(response => {
        this.evtStateOptions = response.data
      })
      this.getDicts('evt_area').then(response => {
        this.areaNameOptions = response.data
      })
      this.getDicts('evt_level').then(response => {
        this.evtLevelOptions = response.data
      })
    },
    methods: {
      getTreeselect() {
        treeselect().then(response => {
          this.violationOptions = response.data
        })
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true
        return data.label.indexOf(value) !== -1
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.violation = data.id
        this.getList()
      },
      showImage(row) {
        this.imgs = []
        const _this = this
        if (row.attachIds && row.attachIds.length > 0) {
          row.attachIds.forEach(item => {
            this.imgs.push({
              url: _this.fileUrl + item.fileId,
              title: item.fileName
            })
          })
          this.dialogVisible = true
          // this.$viewer.show()
        } else {
          this.msgInfo('未上传照片')
        }
      },
      /** 查询案件信息列表 */
      getList() {
        this.loading = true
        if (this.evtSource !== undefined) {
          this.queryParams.evtSource = this.evtSource
          this.showEvtSource = false
        }
        if (this.queryParams.reporterTime !== null && this.queryParams.reporterTime !== undefined) {
          let newQueryParams = {...this.queryParams}
          let newDate = newQueryParams.reporterTime
          newDate = new Date(new Date(newDate).getTime() + 24 * 60 * 60 * 1000 - 1)
          this.queryParams.reporterEndTime = this.dateFormat(newDate)
        }
        listEvent(this.queryParams).then(response => {
          this.eventList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      //时间格式化函数，此处仅针对yyyy-MM-dd hh:mm:ss 的格式进行格式化
      dateFormat(time) {
        let date = new Date(time)
        let year = date.getFullYear()
        /* 在日期格式中，月份是从0开始的，因此要加0
         * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
         * */
        let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1
        let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate()
        let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
        let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()
        let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
        // 拼接
        return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds
      },
      // 案件类型字典翻译
      evtTypeFormat(row, column) {
        return this.selectDictLabel(this.evtTypeOptions, row.evtType)
      },
      evtLevelFormat(row, column){
        return this.selectDictLabel(this.evtLevelOptions, row.evtLevel)
      },
      // 案件来源字典翻译
      evtSourceFormat(row, column) {
        return this.selectDictLabel(this.evtSourceOptions, row.evtSource)
      },
      // 案件状态字典翻译
      evtStateFormat(row, column) {
        return this.selectDictLabel(this.evtStateOptions, row.evtState)
      },
      // 违法、违规信息字典翻译
      violationFormat(row, column) {
        return this.selectDictLabel(this.violationOptions, row.violation)
      },
      // 管辖区字典翻译
      areaNameFormat(row, column) {
        return this.selectDictLabel(this.areaNameOptions, row.areaName)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          evtLevel: undefined,
          evtType: undefined,
          evtSource: undefined,
          evtDate: undefined,
          evtAddress: undefined,
          evtContent: undefined,
          evtState: undefined,
          violation: undefined,
          areaName: undefined,
          x: undefined,
          y: undefined,
          reporterObjectId: undefined,
          reporterObjectName: undefined,
          reporterTime: undefined,
          delFlag: undefined,
          createById: undefined,
          createBy: undefined,
          createTime: undefined,
          updateById: undefined,
          updateBy: undefined,
          updateTime: undefined
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.names = selection.map(item => item.evtCode)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加案件信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getEvent(id).then(response => {
          this.form = response.data
          /** 有些数据是没绑定的修改时显示错误*/
          if (this.form.violation === 0) {
            this.form.violation = null
          }
          this.open = true
          this.title = '修改案件信息'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateEvent(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addEvent(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        const names = row.evtCode || this.names
        this.$confirm('是否确认删除案件号为"' + names + '"的数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delEvent(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        queryParams.isExport = true
        this.$confirm('是否确认导出所有案件信息数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return exportSFEvent(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
<style>
  .imgss img {
    width: 250px !important;
    height: 250px !important;
    /*float: left;*/
  }

</style>
